## Title: LAS utility functions for the modified pylas.
## Modified By: Christian Everhart
## Creation date: Before May 06, 2012

## Modifications
## August 18, 2010 - Started writing code.
## May 06, 2012 - Prepare code for release 0.1 (google code)

import sys, os, mod_pylas, ogr, shutil

def getCopyLAS_ogr(inDIR, inLYR, copyFLD_name):

    if inDIR[-1] != "\\":
        inDIR = inDIR + "\\"
    copyList = []
    
    inFTR = inLYR.GetNextFeature()
    copyFLD_index = inFTR.GetFieldIndex(copyFLD_name)
    if copyFLD_index == -1:
        return copyList
    while(inFTR):
        lasname = inFTR.GetField(copyFLD_index)
        if len(lasname) < 1:
            print "invalid las name attribute value"
            inFTR = inLYR.GetNextFeature()
            continue
        elif len(lasname) >= 1 and len(lasname) < 4:
            lasname = lasname + ".las"
        else:
            if lasname[-4:].upper() != ".LAS":
                lasname = lasname + ".las"
            
        if not os.path.exists(inDIR + lasname):
            print "File: " + inFTR.GetField(copyFLD_index) + " does not exist in the specified directory."
            inFTR = inLYR.GetNextFeature()
            continue
        copyList.append(lasname)
        inFTR = inLYR.GetNextFeature()
    return copyList

def copyLAS(inDIR, outDIR, inLAS):
    if inDIR[-1] != "\\":
        inDIR = inDIR + "\\"
    if outDIR[-1] != "\\":
        outDIR = outDIR + "\\"

    inLAS_path = inDIR + inLAS
    outLAS_path = outDIR + inLAS
    shutil.copy2(inLAS_path, outLAS_path)
    
def moveLAS(inDIR, outDIR, inLAS):
    if inDIR[-1] != "\\":
        inDIR = inDIR + "\\"
    if outDIR[-1] != "\\":
        outDIR = outDIR + "\\"

    inLAS_path = inDIR + inLAS
    outLAS_path = outDIR + inLAS
    shutil.move(inLAS_path, outLAS_path)
    
def removeLAS(inDIR, inLAS):
    if inDIR[-1] != "\\":
        inDIR = inDIR + "\\"

    inLAS_path = inDIR + inLAS
    os.remove(inLAS_path)

## TODO: Write a function that will combine different directories of LAS files
## into a single dataset (directory)

## TODO: Write a function that will combine mutliple bbox/tile layout shapefiles
## into one shapefile

## TODO: Write a function that will log changes (copy, move, remove) in datasets

